package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsUnstructuredDataRelationship;
import com.sg.service.base.component.MOmsUnstructuredDataRelationshipService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsUnstructuredDataRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsUnstructuredDataRelationshipServiceImpl
        extends ServiceImpl<OmsUnstructuredDataRelationshipMapper, OmsUnstructuredDataRelationship>
        implements MOmsUnstructuredDataRelationshipService {

    @Autowired
    OmsUnstructuredDataRelationshipMapper omsUnstructuredDataRelationshipMapper;

    /**
     * 2-7-03查询文档关系详情[8503]
     * gen by moon at 3/27/2024, 12:59:14 AM
     */
    @Trace(operationName = "2-7-03查询文档关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsUnstructuredDataRelationship queryUnstructuredRelationshipDetail(QueryUnstructuredRelationshipDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsUnstructuredDataRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsUnstructuredDataRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsUnstructuredDataRelationship retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-03删除文档关系[8504]
     * gen by moon at 3/27/2024, 12:59:15 AM
     */
    @Trace(operationName = "2-7-03删除文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUnstructuredRelationship(String unstructuredDataRelationshipId) {

        return super.removeById(unstructuredDataRelationshipId);
    }

    /**
     * 2-7-03查询文档关系列表[8505]
     * gen by moon at 3/27/2024, 12:59:18 AM
     */
    @Trace(operationName = "2-7-03查询文档关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataRelationship> queryUnstructuredRelationshipList(QueryUnstructuredRelationshipListReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsUnstructuredDataRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-03新增文档关系[8506]
     * gen by moon at 6/2/2024, 12:23:14 AM
     */
    @Trace(operationName = "2-7-03新增文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addUnstructuredRelationship(OmsUnstructuredDataRelationship omsUnstructuredDataRelationship) {
        omsUnstructuredDataRelationship.setUnstructuredDataRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsUnstructuredDataRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredDataRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredDataRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredDataRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredDataRelationship.setIsValid("FALSE");
        if (super.save(omsUnstructuredDataRelationship)) {
            return omsUnstructuredDataRelationship.getUnstructuredDataRelationshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-7-03批量增文档关系[8794]
     * gen by moon at 3/27/2024, 12:59:19 AM
     */
    @Trace(operationName = "2-7-03批量增文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddUnstructuredRelationship(List<OmsUnstructuredDataRelationship> omsUnstructuredDataRelationship) {

        if (omsUnstructuredDataRelationship == null || omsUnstructuredDataRelationship.size() == 0) {
            return false;
        }

        return super.saveBatch(omsUnstructuredDataRelationship.stream().map(item -> {
            item.setUnstructuredDataRelationshipId(CommonFunctionHelper.getUid());
            item.setIsValid("FALSE");
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-7-03批量查询文档关系By父内容id[8831]
     * gen by moon at 3/27/2024, 7:38:38 PM
     */
    @Trace(operationName = "2-7-03批量查询文档关系By父内容id")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataRelationship> batchQueryUnstructuredRelationship(BatchQueryUnstructuredRelationshipReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataRelationship> queryWrapper = new QueryWrapper<>();

            if (reqDto.getUnstructuredRelationshipList() == null || reqDto.getUnstructuredRelationshipList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("own_parent_content_id", reqDto.getUnstructuredRelationshipList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredDataRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-03批量查询文档关系By归属内容表主键id[8832]
     * gen by moon at 12/12/2024, 4:29:15 PM
     */
    @Trace(operationName = "2-7-03批量查询文档关系By归属内容表主键id")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataRelationship> batchQueryDmAttRela(BatchQueryDmAttRelaReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getUnstructuredRelationshipList() == null || reqDto.getUnstructuredRelationshipList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("own_entity_id", reqDto.getUnstructuredRelationshipList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsUnstructuredDataRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-03修改文档关系[9557]
     * gen by moon at 5/13/2024, 12:07:12 AM
     */
    @Trace(operationName = "2-7-03修改文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUnstructuredRelationship(OmsUnstructuredDataRelationship omsUnstructuredDataRelationship) {

        return super.updateById(omsUnstructuredDataRelationship);
    }

    /**
     * 2-7-03批量查询文档关系by归属工作周期ID[10015]
     * gen by moon at 7/22/2024, 6:07:57 PM
     */
    @Trace(operationName = "2-7-03批量查询文档关系by归属工作周期ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataRelationship> batchQueryUnstructuredRelationshipByOwnWorkCycleId(BatchQueryUnstructuredRelationshipByOwnWorkCycleIdReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataRelationship> queryWrapper = new QueryWrapper<>();

            if (reqDto.getUnstructuredRelationshipList() == null || reqDto.getUnstructuredRelationshipList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("own_work_cycle_id", reqDto.getUnstructuredRelationshipList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredDataRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-03批量改文档关系[10054]
     * gen by moon at 7/25/2024, 9:38:56 PM
     */
    @Trace(operationName = "2-7-03批量改文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateUnstructuredRelationship(List<OmsUnstructuredDataRelationship> omsUnstructuredDataRelationship) {

        if (omsUnstructuredDataRelationship == null || omsUnstructuredDataRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsUnstructuredDataRelationship);

    }
}
